package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IStatementItemService statementItemService;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        statement.setCreateTime(new Date());
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement temp = this.get(statement.getId());
        if (!Statement.STATUS_CONSUME.equals(temp.getStatus())) {
            throw new BusinessException("只有消费中状态的,才可以修改");
        }
        statementMapper.updateByPrimaryKey(statement);

    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = appointmentService.get(appointmentId);

        Statement statement = this.generate(appointment);
        statementMapper.insert(statement);
        return statement;
    }

    public Statement generate(Appointment appointment) {
        Statement statement = new Statement();
        statement.setCreateTime(new Date());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
        statement.setCarSeries(appointment.getCarSeries());//汽车类型
        statement.setCustomerName(appointment.getCustomerName());//客户名称
        statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
        statement.setAppointmentId(appointment.getId());//预约单ID
        statement.setServiceType((long) appointment.getServiceType());//服务类型
        statement.setCustomerPhone(appointment.getCustomerPhone());//用户联系方式
        return statement;
    }


    @Override
    public Statement getByAppointment(Long appointmentId) {
        return statementMapper.selectByAppointment(appointmentId);
    }

    @Override
    public void delete(Long id) {
        Statement statement = this.get(id);
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            statementItemService.deleteByStatementId(id);
            statementMapper.deleteByPrimaryKey(id);
        } else {
            throw new BusinessException("非法操作,只有消费中的状态才可以删除哦");
        }
    }

    @Override
    public void payStatement(Long statementId) {
        Statement statement = this.get(statementId);
        statement.setPayTime(new Date());
        statement.setPayeeId(ShiroUtils.getUserId());
        statement.setStatus(Statement.STATUS_PAID);
        statementMapper.updatePay(statement);
        if (statement.getAppointmentId() != null) {
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal itemPrice) {
        statementMapper.updateAmount(statementId, totalAmount, totalQuantity, itemPrice);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }


    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

}
